<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card class="box-card">
      <el-row :gutter="40">
        <div class="content_title">
          <p></p>
          <span>表格列表</span>
        </div>
      </el-row>
      <el-row :gutter="20" style="margin-top:20px">
        <el-col :span="8">
          <!-- 搜索与添加区域 -->
          <el-input :span="5" placeholder="请输入内容" clearable @clear="getUserList" v-model="querytInfo.query">
            <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogvisible = true">添加用户</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <el-table :data="userList" max-height="560" :border="true" :stripe="true">
        <el-table-column type="index" sortable label="#" fixed width="150">
        </el-table-column>
        <el-table-column prop="id" sortable label="ID" width="150">
        </el-table-column>
        <el-table-column prop="username" label="姓名" width="150">
        </el-table-column>
        <el-table-column prop="email" label="邮箱" width="200">
        </el-table-column>
        <el-table-column prop="mobile" label="电话" width="200">
        </el-table-column>
        <el-table-column prop="role_name" label="角色" width="150">
        </el-table-column>
        <el-table-column prop="" label="添加时间" width="250">
          <template v-slot="scope">
            {{$dateFormat(scope.row.create_time,'yyyy-MM-dd')}}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="200">
          <template v-slot="scope">
            <el-switch v-model="scope.row.mg_state" @change="swithchange(scope.row)">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作" width="232px" fixed="right">
          <template v-slot="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="EditId(scope.row.id)">修改</el-button>
            <el-button type="danger" size="mini" icon="el-icon-delete" @click="removeUserbyid(scope.row.id)">删除</el-button>
            <el-tooltip class="item" effect="dark" content="分配角色" :enterable="false" placement="top">
              <el-button type="warning" size="mini" @click="post_role(scope.row)" icon="el-icon-setting"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="querytInfo.pagenum"
        :page-sizes="[1,2,5,10,15]" :page-size="querytInfo.pagesize" layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>

    <!-- 添加用户对话框-->
    <add_table_user :visible.sync="addDialogvisible" :getUserList="getUserList"></add_table_user>
    <!-- 修改用户对话框-->
    <edit_table_user :visible.sync="editDialogvisible" :getUserList="getUserList" :id="editId"></edit_table_user>
    <!-- 分配角色对话框-->
    <distribu_table_user :visible.sync="distribuDialogvisible" :getUserList="getUserList" :role="dis_role"></distribu_table_user>
  </div>

</template>

<script>
import add_table_user from './modules/table_add_user.vue'
import edit_table_user from './modules/edit_table_user.vue'
import distribu_table_user from './modules/distribu_table_user'
export default {
  data () {
    return {
      querytInfo: {
        query: '',
        // 当前页数
        pagenum: 1,
        // 总条数
        pagesize: 15
      },
      userList: [],
      total: 0,

      // 控制添加对话框的显示与隐藏
      addDialogvisible: false,
      // 控制修改对话框隐藏显示
      editDialogvisible: false,
      // 控制配置对话框隐藏显示
      distribuDialogvisible: false,
      editId: 1,
      dis_role: {}
    }
  },
  components: {
    add_table_user: add_table_user,
    edit_table_user: edit_table_user,
    distribu_table_user: distribu_table_user
  },
  created () {
    this.getUserList()
  },
  methods: {
    EditId (id) {
        this.editId = id, //eslint-disable-line
      this.editDialogvisible = true
    },
    post_role (role) {
        this.dis_role = role, //eslint-disable-line
      this.distribuDialogvisible = true
    },
    getUserList () {
      this.$http.get('/users', {
        params: this.querytInfo
      })
        .then((res) => {
          // console.log(res.data)
          if (res.data.meta.status !== 200) return this.$message.error(res.data.meta.msg)
          this.userList = res.data.data.users
          this.total = res.data.data.total
          setTimeout(() => {
            this.loading = false
          }, 1500)

          // console.log(this.userList)
        })
        .catch((error) => {
          console.log(error)
        })
    },
    // 监听psgesize 改变
    handleSizeChange (newsize) {
      // console.log(newsize);
      this.querytInfo.pagesize = newsize
      this.getUserList()
    },
    // 监听 页码值 改变
    handleCurrentChange (newpage) {
      // console.log(newpage);
      this.querytInfo.pagenum = newpage
      this.getUserList()
    },

    // 监听开关 改变状态
    swithchange (swith) {
      // console.log(swith);
      this.$http.put(`/users/${swith.id}/state/${swith.mg_state}`)
        .then((res) => {
          // console.log(res.data)
          if (res.data.meta.status !== 200) {
            swith.ma_state = !swith.ma_state
            this.$message.error(res.data.meta.msg)
          }
          this.$message.success(res.data.meta.msg)
        })
        .catch((error) => {
          console.log(error)
        })
    },

    // 根据id删除
    removeUserbyid (id) {
      // console.log(id)
      this.$confirm('此操作将永久删除用户，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 删除数据请求
          this.$http.delete('/users/' + id)
            .then((res) => {
              console.log(res.data)
              if (res.data.meta.status !== 200) return this.$message.error(res.data.meta.msg)
              // 刷新数据列表
              this.getUserList()
            })
            .catch((error) => {
              console.log(error)
            })
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>
<style lang="less" scoped="scoped">
  .content_title {
    width: 100%;
    height: 100%;
    display: flex;
    padding-bottom: 20px;
    border-bottom: 1px solid #f5f5f5;

    span {
      font-size: 18px;
      height: 25px;
      line-height: 25px;
      color: #434343;
      display: inline-block;
      margin-left: 10px;
    }

    p {
      width: 5px;
      height: 25px;
      background-color: #34bdfc;
    }
  }

  .el-cascader {
    width: 100%;
  }

  .el-breadcrumb {
    margin: 0 0 10px 0;
  }

  .el-card {
    margin-top: 20px;
    height: calc(100vh - 195px);
  }

  .el-pagination {
    margin-top: 20px;
  }

  .el-table {
    margin-top: 20px;
  }
</style>
